import {
  FILL_SEARCH_HISTORY,
  FILL_SEARCH_SUGGEST,
  FILL_SEARCH_RESULT,
  FILL_HOT_WORDS,
  FILL_MATCH_LIST,
  CLEAR_SEARCH_RESULT
} from "./types";

const defaultState = {
  hotWords: [], // 热搜词
  searchResult: {
    songs: [],
    matchs: []
  }, // 搜索结果
  searchHistory: [], // 搜索历史
  searchSuggestWords: [], //搜索建议
  matchlist: [] // 最佳匹配
};
export default function search(state = defaultState, action) {
  switch (action.type) {
    case FILL_HOT_WORDS /** 热搜词 */:
      return {
        ...state,
        hotWords: action.data
      };
    case FILL_SEARCH_SUGGEST /** 搜索建议词 */:
      return {
        ...state,
        searchSuggestWords: action.data
      };
    case FILL_SEARCH_RESULT /** 搜索结果 */:
      const { matchs, songs } = action.data;
      const preSongs = [...state.searchResult.songs];
      const finalSongs = [...preSongs, ...songs];
      finalSongs.songCount = songs.songCount;
      return {
        ...state,
        searchResult: {
          matchs,
          songs: finalSongs
        }
      };
    case CLEAR_SEARCH_RESULT /** 清空搜索结果 */:
      return {
        ...state,
        searchResult: {
          songs: [],
          matchs: []
        }
      };
    default:
      return state;
  }
}
